﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LoadShould.cs" company="SaltFx">
//   Copyright (c) SaltFx - All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace SaltFx.Data.RavenDB.Tests.Repository
{
	using NUnit.Framework;

	using StructureMap;

	/// <summary>
	/// The Repository Load method tests.
	/// </summary>
	[TestFixture]
	public class LoadShould
	{
		#region Public Methods and Operators

		/// <summary>
		/// Tests that Load will load a document based on id.
		/// </summary>
		[Test]
		public void LoadById()
		{
			// Arrange
			Bootstrapper.Start();
			const string Key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

			var repository = ObjectFactory.GetInstance<Repository<TestModel>>();
			var unitOfWorkFactory = ObjectFactory.GetInstance<IUnitOfWorkFactory>();
			var unitOfWork = unitOfWorkFactory.CreateUnitOfWork();

			var testModel = new TestModel(Key);

			using (unitOfWork.Start())
			{
				repository.Create(testModel);

				unitOfWork.Commit();
			}

			string foundModelId;

			using (unitOfWork.Start())
			{
				var foundModel = repository.Find.One(x => x.Key.Equals(Key));

				foundModelId = foundModel.Id;
			}

			// Act
			using (unitOfWork.Start())
			{
				var foundModel = repository.Load(foundModelId);

				// Assert
				Assert.That(foundModel.Key == Key);
			}
		}

		#endregion
	}
}